#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    flag=false;
    srand(time(NULL));
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&MainWindow::setTimerText);
    connect(ui->action1, &QAction::triggered, this, &MainWindow::onActionTriggered);
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_stratbtn_clicked()
{
    if(!flag)
    {
        flag=true;
        timer->start(50);
        ui->stratbtn->setText("暂停");
        ui->action1->setEnabled(false);
    }else{
        flag=false;
        timer->stop();
        ui->stratbtn->setText("开始");
        ui->action1->setEnabled(true);
    }
}




void MainWindow::setTimerText()
{

    int x = rand() % 13;
    ui->username->setText(user[x]);
}

void MainWindow::onActionTriggered()
{
    QString fileName = QFileDialog::getOpenFileName(nullptr, QObject::tr("Open File"),
                                                    QDir::homePath(), QObject::tr("All Files (*)"));

    if (!fileName.isEmpty()) {
        // 文件打开成功，处理文件
        // 例如：QFile file(fileName);
        // ...
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            // 处理错误，例如可以抛出异常或者返回一个错误标志
            //return QString();
        }


        QTextStream in(&file);
        QString text = in.readAll();
        qDebug()<<text;
        file.close();


    }
}
